<template>
  <el-card>
    <el-row>
      <el-col :span="16" class="left">
        <KGraph id="relation" :kg_data="kg_data"></KGraph>
      </el-col>
      <el-col :span="8" class="right">
        <div class="search">
            <el-input v-model="input" placeholder="请输入内容"></el-input>
            <el-button @click="toSearchNodes">搜索</el-button>
        </div>
        <div>
          <p style="margin-left: 8px; font-size: 24px;">
            输入内容或关键词，<br>
            系统会去寻找所有包含改内容的节点<br>
            和相对应的关系
          </p>
        </div>
      </el-col>
    </el-row>
  </el-card>
</template>

<script>
import KGraph from '@/components/EchartGraphs/KGraph/index.vue'
import { Message } from 'element-ui'
import { getAllGraph, searchGraphNodes } from '@/api/graph'

export default {
  components: { KGraph },
  mounted() {
    this.getAllKGGraph()
  },
  methods: {
    toSearchNodes: function() {
      if(!this.input) return this.getAllKGGraph()
      searchGraphNodes({key: this.input}).then(resp => {
        if(resp.code !== 200) return Message.error(resp.message)
        this.$set(this, 'kg_data', resp.data)
      Message.success(resp.message)
      })
    },
    getAllKGGraph: function() {
      getAllGraph().then(resp => {
        if(resp.code === 200) this.$set(this, 'kg_data', resp.data)
        else Message.error(resp.message)
      })
    }
  },
  data() {
    return {
      input: '',
      kg_data: {
        categories: [{ name: '贾家荣国府' }, { name: '贾家宁国府' }],
        nodes: [
          { category: 1, id: 0, name: '贾政' }, { category: 0, id: 1, name: '赵姨娘' },
          { category: 0, id: 2, name: '贾探春' }, { category: 0, id: 3, name: '贾探春' },
          { category: 0, id: 4, name: '贾探春' }
        ],
        links: [
          { source: 1, target: 2, value: '母亲' }, { source: 0, target: 2, value: '父亲' },
          { source: 2, target: 1, value: '女儿' }, { source: 2, target: 0, value: '女儿' },
          { source: 1, target: 0, value: '女儿' }, { source: 4, target: 0, value: '女儿' }
        ]
      }
    }
  }
}
</script>

<style lang="scss">
.left {
  #relation {
    height: 600px;
  }
}
.right {
  .search {
    display: flex;
    justify-content: space-around;
    width: 380px;
    .el-input {
      margin-right: 10px;
    }
  }
  #tagsList {
    position: relative;
    width: 250px;
    height: 250px;
    & a {
      position: absolute;
      top: 0px;
      left: 0px;
      font-family: Microsoft YaHei;
      color: gray;
      font-weight: bold;
      text-decoration: none;
      padding: 3px 6px;
    }
    & a:hover {
      color: #249e92;
      letter-spacing: 2px;
    }
  }
}
</style>
